<?php

namespace exphp;

use SimpleXMLElement;

class DataTypeConvert
{
    /**
     * 任意数据格式转数组
     * @param any $variate 待转换的值
     * @param string $delimiter 切割符，默认为英文逗号，当待转换的值为string型生效
     * @return array
     */
    public static function convertToArray($variate, $delimiter = ',')
    {
        if (is_string($variate)) {
            if ($variate === '') {
                return array();
            }
            $objcet = simplexml_load_string($variate, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);
            if (is_object($objcet)) {
                return self::convertToArray($objcet, $delimiter);
            }
            $array = json_decode($variate, true);
            if (is_array($array)) {
                return self::convertToArray($array, $delimiter);
            }
            $array = explode($delimiter, $variate);
            if (is_array($array) && count($array) > 1) {
                return self::convertToArray($array, $delimiter);
            }
            return self::convertToArray(array($variate), $delimiter);
        } elseif (is_numeric($variate)) {
            if (is_float($variate)) {
                return self::convertToArray((string) $variate, $delimiter);
            }
            return array($variate);
        } elseif (is_object($variate)) {
            return self::convertToArray(json_encode($variate), $delimiter);
        } elseif (is_null($variate)) {
            return '';
        } else {
            return $variate;
        }
    }

    /**
     * 任意格式转换成Json
     * @param any $variate 带转换的内容
     * @param bool $isFormatting 是否格式化输出
     * @return string 转化后的格式
     */
    public static function convertToJson($variate, $isFormatting = false)
    {
        $variate = self::convertToArray($variate);
        if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
            if ($isFormatting) {
                return json_encode($variate, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
            } else {
                return json_encode($variate, JSON_UNESCAPED_UNICODE);
            }
        } else {
            return json_encode($variate);
        }
    }

    /**
     * 任意格式转换成对象
     * @param any $variate 带转换的内容
     * @param string $delimiter 分割字符
     * @return object 转化后的格式
     */
    public static function convertToObject($variate, $delimiter = ',')
    {
        if (is_object($variate)) {
            return $variate;
        }
        return (object) self::convertToArray($variate, $delimiter);
    }

    /**
     * 任意格式转换成XML
     * @param any $variate 带转换的内容
     * @param string $rootElement XML根部
     * @param any $xml 递归传入使用，请勿传入任何数据
     * @return string 转化后的格式
     */
    public static function convertToXml($variate, $rootElement = 'xml', $xml = null)
    {
        $array = self::convertToArray($variate);
        $xmlObj = $xml;
        if ($xml === null) {
            $xmlObj = new SimpleXMLElement("<{$rootElement}/>");
        }
        array_walk($array, function ($value, $key) use (&$xmlObj) {
            if (is_array($value)) {
                self::convertToXml($value, $key, $xmlObj->addChild($key));
            } else {
                $xmlObj->addChild($key, $value);
            }
        });
        return $xmlObj->asXML();
    }

    /**
     * 任意格式转换成字符串
     * @param any $variate 带转换的内容
     * @param string $delimiter 分割字符
     * @return string 转化后的格式
     */
    public static function convertToString($variate, $delimiter = ',')
    {
        if (is_string($variate)) {
            return $variate;
        }
        return implode($delimiter, self::convertToArray($variate, $delimiter));
    }

    /**
     * 格式化数字
     * @param any $variate 带转换的内容
     * @param integer $decimals 保留几位小数
     * @param boolean $isFormatting 千分格式化,每三位以逗号隔开
     * @return string 格式化后的数字
     */
    public static function numberFormatter($variate, $decimals = 0, $isFormatting = false)
    {
        if (!is_numeric($variate)) {
            // OutPut::errorMessage('Invalid Format', '只能传入数字[numberFormatter 格式化数字错误]');
        }
        if ($isFormatting) {
            return number_format($variate, $decimals);
        } else {
            return number_format($variate, $decimals, '.', '');
        }
    }

    /**
     * 替换字符串
     * @param array $rule 替换规则 ['原始值' => '替换后的值']
     * @param string $str 被替换的字符串
     * @return string 替换后的字符串
     */
    public static function replaceString($rule, &$str)
    {
        return $str = str_replace(array_keys($rule), array_values($rule), $str);
    }
}
